using UnityEngine;

public class PlayerAIState_Defense_Defense : State<PlayerAI>
{
	public static PlayerAIState_Defense_Defense instance;

	private PlayerAIState_Defense_Defense()
	{
	}

	public static PlayerAIState_Defense_Defense getInstance()
	{
		if (instance == null)
		{
			instance = new PlayerAIState_Defense_Defense();
		}
		return instance;
	}

	public override void enter(PlayerAI owner)
	{
		base.enter(owner);
		owner.player.do_defense();
	}

	public override void fixedUpdate(PlayerAI owner)
	{
		base.fixedUpdate(owner);
		defense(owner);
		//if (TutorialController.preTutorialType == TutorialType.Game_Steal)
		//{
		//	owner.do_idle();
		//}
	}

	private void defense(PlayerAI owner)
	{
		if (GameHelper.IsInThreePointArea(owner.player.getParaPlayerPositon(), owner.player.getOffenseDirection() == OffenseDirection.RIGHT))
		{
			getDefensePosInThree(owner);
		}
		else
		{
			getDefensePosInTwo(owner);
		}
		if (!GameHelper.isReachTargetPos(owner.player, owner.defense_defense_pos_target))
		{
			if (owner.time_Stop > 0f)
			{
				if (owner.player.team.is_to_defensepos_when_para_closer_to_basketry && MathHelper.getSqrDistance(owner.player.getPosition(), owner.player.getDefenseBasketPos()) > MathHelper.getSqrDistance(owner.player.getParaPlayerPositon(), owner.player.getDefenseBasketPos()))
				{
					owner.time_Stop = 0f;
				}
				owner.time_Stop -= Time.fixedDeltaTime;
				return;
			}
			if (MathHelper.getSqrDistance(owner.player.getPosition(), owner.defense_defense_pos_target) < 4f)
			{
				GameHelper.MoveTo(owner.player, owner.defense_defense_pos_target, owner.player.getMoveSpeed(), isLookAtTargetPos: false);
				owner.player.LookAtSmooth(owner.player.paraPlayer.getPosition());
			}
			else
			{
				GameHelper.MoveTo(owner.player, owner.defense_defense_pos_target, owner.player.getMoveSpeed(), isLookAtTargetPos: true);
			}
			owner.player.do_run();
			owner.defense_defense_time_steal = 0f;
			return;
		}
		if (!owner.player.isInState(PlayerStateBehavior_Defense.getInstance()))
		{
			owner.time_Stop = owner.player.team.time_reaction_to_defensepos;
		}
		owner.player.do_defense();
		if (owner.player.paraPlayer.isControlBall)
		{
			if (GameHelper.IsInThreePointArea(owner.player.getParaPlayerPositon(), owner.player.getOffenseDirection() == OffenseDirection.RIGHT))
			{
				if (owner.defense_defense_time_chase >= owner.player.team.time_out_three_to_steal)
				{
					owner.defense_defense_time_steal += Time.fixedDeltaTime;
				}
			}
			else
			{
				owner.defense_defense_time_steal += Time.fixedDeltaTime;
			}
		}
		if (owner.defense_defense_time_steal >= 1f && owner.player.paraPlayer.isControlBall)
		{
			owner.do_defense_steal();
			owner.defense_defense_time_steal = 0f;
		}
	}

	private void getDefensePosInThree(PlayerAI owner)
	{
		if ((owner.player.paraPlayer.isInState(PlayerStateBehavior_Dribble_Stay.getInstance()) || owner.player.paraPlayer.isInState(PlayerStateBehavior_UpDownUp.getInstance())) && (GameData.IsFinishTutorial /*|| TutorialController.preTutorialType == TutorialType.Game_RealGame*/))
		{
			owner.defense_defense_time_chase += Time.fixedDeltaTime;
		}
		if (owner.defense_defense_time_chase >= owner.player.team.time_out_three_to_steal)
		{
			owner.defense_defense_pos_target = DSS.GetDefensePos(MathHelper.setY(owner.player.getParaPlayerPositon(), 0f), owner.player.getOffenseDirection() == OffenseDirection.RIGHT);
			return;
		}
		Vector3 defenseBasketPos = owner.player.getDefenseBasketPos();
		float z = defenseBasketPos.z;
		Vector3 paraPlayerPositon = owner.player.getParaPlayerPositon();
		float z2 = paraPlayerPositon.z;
		Vector3 defenseBasketPos2 = owner.player.getDefenseBasketPos();
		float num = 7.25f * (z2 - defenseBasketPos2.z);
		Vector3 paraPlayerPositon2 = owner.player.getParaPlayerPositon();
		float x = paraPlayerPositon2.x;
		Vector3 paraPlayerPositon3 = owner.player.getParaPlayerPositon();
		float num2 = x * paraPlayerPositon3.x;
		Vector3 paraPlayerPositon4 = owner.player.getParaPlayerPositon();
		float z3 = paraPlayerPositon4.z;
		Vector3 defenseBasketPos3 = owner.player.getDefenseBasketPos();
		float num3 = z3 - defenseBasketPos3.z;
		Vector3 paraPlayerPositon5 = owner.player.getParaPlayerPositon();
		float z4 = paraPlayerPositon5.z;
		Vector3 defenseBasketPos4 = owner.player.getDefenseBasketPos();
		float num4 = z + num / Mathf.Sqrt(num2 + num3 * (z4 - defenseBasketPos4.z));
		Vector3 paraPlayerPositon6 = owner.player.getParaPlayerPositon();
		float x2 = paraPlayerPositon6.x;
		float num5 = num4;
		Vector3 defenseBasketPos5 = owner.player.getDefenseBasketPos();
		float num6 = x2 * (num5 - defenseBasketPos5.z);
		Vector3 paraPlayerPositon7 = owner.player.getParaPlayerPositon();
		float z5 = paraPlayerPositon7.z;
		Vector3 defenseBasketPos6 = owner.player.getDefenseBasketPos();
		float x3 = num6 / (z5 - defenseBasketPos6.z);
		owner.defense_defense_pos_target = new Vector3(x3, 0f, num4);
		if (Vector3.SqrMagnitude(owner.defense_defense_pos_target - owner.player.getParaPlayerPositon()) < 1f)
		{
			owner.defense_defense_pos_target = DSS.GetDefensePos(MathHelper.setY(owner.player.getParaPlayerPositon(), 0f), owner.player.getOffenseDirection() == OffenseDirection.RIGHT);
		}
	}

	private void getDefensePosInTwo(PlayerAI owner)
	{
		owner.defense_defense_pos_target = DSS.GetDefensePos(MathHelper.setY(owner.player.getParaPlayerPositon(), 0f), owner.player.getOffenseDirection() == OffenseDirection.RIGHT);
		owner.defense_defense_time_chase = 0f;
	}
}
